package com.tc.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

//@Configuration
public class DruidDataSourceConfig {
     //赋值给这个配置对象
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druid(){
        return new DruidDataSource();
    }

    @Bean
    public ServletRegistrationBean druidServlet(){
        //实例化这个bean
        ServletRegistrationBean servletRegistrationBean=
                new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        //IP白名单
        servletRegistrationBean.addInitParameter("allow","*");
        //登录的用户名和密码
        servletRegistrationBean.addInitParameter("loginUsername","tangcong");
        servletRegistrationBean.addInitParameter("loginPassword","123456");
        return servletRegistrationBean;
    }
    // 启动网站的连接 http://localhost:8080/druid/login.html
    //配置过滤器
    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean(new WebStatFilter());
        //过滤所有的访问url
        filterRegistrationBean.addUrlPatterns("/*");
        //排除js gif png 等图片 静态文件
        filterRegistrationBean.addInitParameter("exclusions","*.js,*gif,*.png,*css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}
